package top.tec.common.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.tec.common.properties.SaTokenProperties;

/**
 * sa-token 配置
 *
 * @author Lion Li
 */
@AutoConfiguration
@EnableConfigurationProperties(SaTokenProperties.class)
@RequiredArgsConstructor
public class SaTokenConfig implements WebMvcConfigurer {

    private final SaTokenProperties properties;

    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(
                        new SaInterceptor(handle -> {
                            SaRouter.match("/**").check(r -> StpUtil.checkLogin());
                        })
                )
                .addPathPatterns("/**").excludePathPatterns(properties.getExcludes());
    }

}
